<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="el-card__header">
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
            <el-breadcrumb-item>活动列表</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <!-- 头部搜索新增 -->
        <el-form ref="form">
          <div class="above">
            <div class="left">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="标签名称" label-width="80px">
                    <el-input v-model="inputVal" placeholder="" style="width: 180px;"></el-input>
                  </el-form-item></el-col>
                <el-col :span="10">
                  <el-form-item label="状态" label-width="80px">
                    <el-select v-model="selectVal" placeholder="请选择">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                    </el-select>
                  </el-form-item></el-col>
                <el-col :span="6">
                  <el-form-item>
                    <el-button style="margin-left: 10px;" @click="resetForm">清除</el-button>
                    <el-button type="primary" @click.enter="search">搜索</el-button>
                </el-form-item></el-col>
              </el-row>
            </div>
            <div>
              <el-link v-show="this.$route.query.id" type="primary" :underline="false" style="margin-right: 10px;" @click="$router.push('/subjects/list')">← 返回学科</el-link>
              <el-button type="success" icon="el-icon-edit" size="small" @click="addCatalogue">新增标签</el-button>
            </div>
          </div>
        </el-form>
        <!-- 提示组件 -->
        <el-alert
        :title="alertText"
        type="info"
        :closable="false"
        show-icon style="margin-bottom: 20px;">
        </el-alert>
        <!-- 表格 -->
        <el-table style="width: 100%" :data="list">
          <el-table-column label="序号" width="70" type="index"/>
          <el-table-column label="所属学科" prop="subjectName"/>
          <el-table-column label="标签名称" prop="tagName"/>
          <el-table-column label="创建者" prop="username" />
          <el-table-column label="创建日期" prop="addDate">
            <template #default="{ row }">
              {{ formatterData(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state" :formatter="formatterState"/>
          <el-table-column label="操作" fixed="right">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="updateState(row)" v-if="row.state === 1 ">禁用</el-button>
              <el-button type="text" size="small" @click="updateState(row)" v-else>启用</el-button>
              <el-button type="text" size="small" :disabled="row.state === 1" @click="showModify(row)">修改</el-button>
              <el-button type="text" size="small" :disabled="row.state === 1" @click="removeTags(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="pager" style="margin:10px;display:flex;justify-content:flex-end">
              <el-pagination
                background
                :current-page="pages.page"
                :page-sizes="[2, 4, 6, 8, 10]"
                :page-size="pages.pageSize"
                layout="total,sizes,prev,pager,next,jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
        </div>
      </el-card>
      <!-- 新增组件 -->
      <AddTags :show-dialog.sync="showDialog"></AddTags>
      <!-- 修改弹窗 -->
      <el-dialog title="修改目录" :visible="ShowDialog2" width="400px" top="16vh" @close="closeDialog2">
        <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
            <el-select v-model="form.subjectID" style="width:100%" placeholder="请选择">
              <el-option
              v-for="item in optionsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="form.tagName" style="width:100%" placeholder="请输入目录名称"></el-input>
          </el-form-item>
        </el-form>
        <template v-slot:footer>
          <el-button  @click="closeDialog2">取消</el-button>
          <el-button type="primary" @click="SureModify">确定</el-button>
        </template>
        </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, remove, detail, update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects.js'
import AddTags from '../components/tags-add.vue'
import dayjs from 'dayjs'
export default {
  name: 'Tags',
  components: { AddTags },
  data () {
    return {
      inputVal: '',
      selectVal: '',
      options: [{
        value: '1',
        label: '启用'
      }, {
        value: '0',
        label: '禁用'
      }],
      list: [],
      pages: {
        page: 1, // 当前页
        pageSize: 10 // 每页条数
      },
      parmas: {
        tagName: '',
        state: ''
      },
      counts: 0,
      alertText: '',
      checked: true,
      showDialog: false,
      ShowDialog2: false,
      form: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ]
      },
      optionsList: []
    }
  },
  created () {
    this.subject()
  },
  mounted () {
    this.listApi()
  },
  methods: {
    /**
     * 接收学科参数
     */
    async subject () {
      if (this.$route.query) {
        // console.log(this.$route.query)
        this.pages.subjectID = this.$route.query.id
        await list(this.pages)
        // console.log(res)
      }
    },
    /**
     * 点击修改确定按钮
     */
    SureModify () {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.$route.query.id) {
          this.form.subjectID = Number(this.$route.query.id)
        }
        await update(this.form)
        this.$message.success('修改成功')
        this.closeDialog2()
        this.listApi()
        this.$refs.addForm.resetFields()
      })
    },
    /**
     * 点击修改按钮
     */
    async showModify (obj) {
      this.clickShowDialog()
      this.form.id = obj.id
      const res = await detail(obj)
      // console.log(res)
      this.form.tagName = res.data.tagName
      this.form.subjectID = res.data.subjectID
      // console.log(this.form.tagName, this.form.subjectID)
      const { data } = await simple()
      this.optionsList = data
    },
    /**
     * 关闭修改弹窗
     */
    closeDialog2 () {
      this.ShowDialog2 = false
    },
    /**
     * 关闭新增弹窗
     */
    closeDialog () {
      this.showDialog = false
    },
    /**
     * 显示修改弹窗
     */
    clickShowDialog () {
      this.ShowDialog2 = true
    },
    /**
     * 显示新增弹窗
     */
    addCatalogue () {
      this.showDialog = true
    },
    /**
     * 删除按钮
     */
    removeTags (id) {
      // console.log(id)
      this.$confirm('确定删除吗？', '温馨提示').then(async () => {
        await remove({ id })
        if (this.list.length === 1 && this.pages.page > 1) {
          this.pages.page--
        }
        this.$message.success('删除成功')
        this.listApi()
      }).catch((error) => {
        console.log(error)
      })
    },
    /**
     * 操作
     */
    updateState (obj) {
      // console.log(obj)
      obj.state = obj.state === 1 ? 0 : 1
    },
    /**
     * 搜索按钮
     */
    async search () {
      this.parmas.tagName = this.inputVal
      this.parmas.state = this.selectVal
      const { data } = await list(this.parmas)
      // console.log(res)
      this.list = data.items
    },
    /**
     * 清空按钮
     */
    resetForm () {
      this.inputVal = ''
      this.selectVal = ''
      this.listApi()
    },
    /**
     * 页码变化
     */
    handleCurrentChange (val) {
      this.pages.page = val
      this.listApi()
    },
    /**
     * 页容量变化
     */
    handleSizeChange (val) {
      this.pages.pagesize = val
      this.pages.page = 1
      this.listApi()
    },
    /**
     * 处理时间
     */
    formatterData (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    /**
     * 处理状态
     */
    formatterState (row, column, cellValue, index) {
      if (Number(cellValue === 1)) {
        return '已启用'
      } else if (Number(cellValue === 0)) {
        return '已禁用'
      }
    },
    /**
     * 获取列表渲染
     */
    async listApi () {
      const { data } = await list(this.pages)
      // console.log(data)
      this.counts = data.counts
      this.alertText = `数据一共 ${this.counts} 条`
      this.list = data.items
      this.counts = data.counts
    }
  }
}
</script>

<style scoped lang="scss">
  .above {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
   ::v-deep.el-card__header {
  padding: 0px 15px 20px 20px;
  box-sizing: border-box;
  border-bottom: 1px solid #ebeef5;
}
</style>
